﻿using B.Z.BasicData.Domain.Entity.Authorize;
using B.Z.BasicData.ErrorCode;
using B.Z.BasicData.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.Z.BasicData.API.Read.Application.User
{
    public class CityQueryCommandHandler : IRequestHandler<CityQueryCommand, APIResult<List<T_City>>>
    {
        private readonly IBaseRepository<T_City> cityRepository;

        public CityQueryCommandHandler(IBaseRepository<T_City> cityRepository)
        {
            this.cityRepository = cityRepository;
        }

        public Task<APIResult<List<T_City>>> Handle(CityQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<T_City>> result = new APIResult<List<T_City>>();
            result.Code = APIEnum.ok;
            result.Message = "获取成功";

            var list = cityRepository.GetAll().AsNoTracking().Where(p => !p.IsDelete && p.T_ProvinceId == request.ProvinceId);
            result.Data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
